import time
# import ssd1306
# from machine import I2C, Pin
import pygame as pg

pg.init()
pg.font.init()
font = pg.font.SysFont('Times New Roman', 10)
color = pg.Color('dodgerblue2')
print("create display and button")

# display
win = pg.display.set_mode((128, 64))
# i2c = I2C(scl=Pin(2), sda=Pin(14)) # D4 D5
# display = ssd1306.SSD1306_I2C(128, 64, i2c)
# flip screen
# display.write_cmd(0xA0) # SEGREMAP
# display.write_cmd(0xC0) # COMSCANINC

# port: D8
# button = Pin(15, Pin.IN)

# Ask user Yes or No
def question(msg, neg=True, count=3000):
	ans = 1

	while(count > 0):
		# display.fill(0)
		win.fill((255, 255, 255))

		text1 = font.render(msg, True, color)
		# 设置坐标（20，10）
		win.blit(text1,(20, 10))
		# display.text(msg,20,10,1)

		count -= 100
		text2 = font.render(str(count//1000+1), True, color)
		win.blit(text2, (40, 20))
		# display.text(str(count//1000+1),40,20,1)

		text3 = font.render("sec", True, color)
		win.blit(text3, (60, 20))
		# display.text("sec",60,20,1)

		text4 = font.render("sec", True, color)
		win.blit(text4, (60, 20))

		text5 = font.render("Yes", True, color)
		win.blit(text5, (30, 40))
		# display.text("Yes",30,40,1)

		text5 = font.render("No", True, color)
		win.blit(text5, (75, 40))
		# display.text("No",75,40,1)

		# read input
		# ans = button.value()
		# 这个还要再改
		ans = 1

		if(neg):
			ans = not ans

		# show rect on yes or no
		rect1 = pg.Rect(26, 36, 32, 16)
		pg.draw.rect(win, color, rect1, 1)
		# display.rect(26, 36, 32, 16, ans)

		rect2 = pg.Rect(66, 36, 32, 16)
		pg.draw.rect(win, color, rect2, 1)
		# display.rect(66, 36, 32, 16, not ans)

		pg.display.update()
		# display.show()

		# sleep
		time.sleep(0.1)
		# time.sleep_ms(100)
	return True
	# return ans

#  Welcom screen
def welcome():
	# display.fill(1)
	# time.sleep(0.5)
	# display.show()
	win.fill((255, 255, 255))
	# display.fill(0)

	pg.draw.line(win, color, (0, 0), (0, 127), 1)
	# display.hline(0,0,127,1)
	pg.draw.line(win, color, (0, 63), (127, 63), 1)
	# display.hline(0,63,127,1)

	pg.draw.line(win, color, (0, 0), (127, 0), 1)
	# display.vline(0,0,63,1)
	pg.draw.line(win, color, (127, 0), (127, 63), 1)
	# display.vline(127,0,63,1)

	text1 = font.render('OM Robot v1.0', True, color)
	win.blit(text1, (20, 10))
	# display.text('OM Robot v1.0', 20, 10, 1)

	text2 = font.render('SCUT 2018', True, color)
	win.blit(text2, (20, 45))
	# display.text('SCUT 2018', 20, 45, 1)

	frame = 0
	for i in range(10):
		for event in pg.event.get():
			if event.type == pg.QUIT:
				pg.quit()
		text1 = font.render('o', True, color)
		win.blit(text1, (55+2, 23))
		# display.text("o", 55+2,   23, 1)
		# erase
		# text2 = font.render('M', True, color)
		# win.blit(text2, (55, 23+8))
		# display.text("M", 55, 23+8, 0)

		# text3 = font.render('V', True, color)
		# win.blit(text3, (55, 23+8))
		# display.text("V", 55, 23+8, 0)

		# draw
		if frame == 1:
			print("M")
			text2 = font.render('M', False, color)
			text3 = font.render('V', True, pg.Color("White"))
			win.blit(text2, (55, 23+8))
			win.blit(text3, (55, 23+8))
		# display.text("M", 55, 23+8, 1)
		else:
			print("V")
			text2 = font.render('M', True, pg.Color("White"))
			text3 = font.render('V', True, color)
			win.blit(text2, (55, 23+8))
			win.blit(text3, (55, 23+8))
		# display.text("V", 55, 23+8, 1)

		frame = not frame
		pg.display.update()
		# display.show()
		time.sleep(0.1)

def bye():
	win.fill((255, 255, 255))
	# display.fill(0)
	text = font.render('  Command mode', True, color)
	win.blit(text, (10, 20))
	# display.text('  Command mode', 10, 20, 1)
	text = font.render('Type your cmds', True, color)
	win.blit(text, (10, 40))
	# display.text('Type your cmds now', 10, 45, 1)

	pg.display.update()
	# display.show()
	time.sleep(1)

